package com.bbx.taxi.client.Task;

import java.util.ArrayList;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;

import com.bbx.androidapi.util.SharedPreUtil;
import com.bbx.api.gclient.GMsg;
import com.bbx.api.sdk.model.passanger.MyOrder;
import com.bbx.api.sdk.model.passanger.Token;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.MyOrderList;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.MyOrders;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.passeger.conn.MyOrderNet_Unfini;
import com.bbx.taxi.client.Task.BaseAsyncTask;
import com.bbx.taxi.client.Util.IsSnQiangDan;
import com.bbx.api.util.SharedPreEncryptUtil;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.client.Activity.Base.BaseBBXActivity;
import com.bbx.taxi.client.Activity.Menu.MyOrderActivity;
import com.bbx.taxi.client.Bean.Message.Publisher;
import com.bbx.taxi.client.DB.MyOrder.MyOrderingDB;

public class MyOrderingTask {
	private Context context;
	public static MyOrderingTask instance;
	public MyOrderTask_Unfini mMyOrderTask_Unfini;
	private MyOrderingDB db_myordering;// 我的进行中的订单数据库
	private ArrayList<MyOrderList> list;
	String jmsg_data;
	JSONObject data;
	int type;
	private boolean isload=false;
	public interface OnOrderingFinishListener {
		public void onOrderingFinish(ArrayList<MyOrderList> list);
	}
	public interface OnOrderingFinishToMessageListener {
		public void onOrderingFinishToMessage(ArrayList<MyOrderList> list,int type,String jmsg_data,JSONObject data);
	}
	static OnOrderingFinishToMessageListener mOnOrderingFinishToMessageListener;
	static ArrayList<OnOrderingFinishListener> mOnOrderingFinishListener = new ArrayList<MyOrderingTask.OnOrderingFinishListener>();

	public static void setOnOrderingFinishListener(OnOrderingFinishListener l) {
		if (!mOnOrderingFinishListener.contains(l))
			mOnOrderingFinishListener.add(l);
	}
	public static void removeOnOrderingFinishListener(OnOrderingFinishListener l) {
		mOnOrderingFinishListener.remove(l);
	}
	public static void setOnOrderingFinishToMessageListener(OnOrderingFinishToMessageListener l) {
		mOnOrderingFinishToMessageListener=l;
	}
	public static MyOrderingTask getInstance(Context context) {
		if(instance==null){
			instance = new MyOrderingTask(context);
		}else{
			instance.getMyOrdering();
		}
		return instance;
	}
	public static MyOrderingTask getInstance(Context context,int type,String jmsg_data,JSONObject data) {
		instance = new MyOrderingTask(context,type,jmsg_data,data);
		return instance;
	}
	public MyOrderingTask(Context context) {
		this.context = context;
		getMyOrdering();
	}
	public MyOrderingTask(Context context,int type,String jmsg_data,JSONObject data) {
		this.context = context;
		db_myordering = new MyOrderingDB();
		this.jmsg_data=jmsg_data;
		this.data=data;
		this.type=type;
		getMyOrdering();
	}
	/**
	 * 获取进行中的订单
	 */
	public void getMyOrdering(){
		db_myordering = new MyOrderingDB();
		if(isload){
			return;
		}
		list=null;
		Token token = SharedPreEncryptUtil.getStringEncryptValue(context,
				SharedPreEncryptUtil.TokenAndUid, "");
		if(token!=null&&token.getUid()!=null&& token.getAccess_token()!=null
				&&!token.getUid().equals("")&&!token.getAccess_token().equals("")){
		MyOrder myOrder = new MyOrder(context);
		myOrder.uid = token.getUid();
		myOrder.access_token = token.getAccess_token();
		myOrder.start_id = 0;
		myOrder.count = MyOrderActivity.num_ing;
		myOrder.direction = 0;
		mMyOrderTask_Unfini = new MyOrderTask_Unfini(context, myOrder);
		mMyOrderTask_Unfini.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);	
		}
	}

	/**
	 * 进行中的订单
	 */
	private class MyOrderTask_Unfini extends BaseAsyncTask {

		private MyOrder myOrder;

		public MyOrderTask_Unfini(Context context, MyOrder myOrder) {
			super(context,false,0);
			this.myOrder = myOrder;
		}

		@Override
		public void onSuccess(int status,Object object) {
			isload=false;
			if (object != null) {
				MyOrders myOrders = (MyOrders) object;
				if (myOrders.getList() != null && !myOrders.getList().isEmpty()) {
					list = myOrders.getList();
//					// 存储数据库
					db_myordering.onWriteDB_ing(
							myOrders.getList(),mHandler);
				
				} else {
					if (!mOnOrderingFinishListener.isEmpty()) {
						for (int i = 0; i < mOnOrderingFinishListener.size(); i++) {
							mOnOrderingFinishListener.get(i).onOrderingFinish(null);
						}
				      }
					if (mOnOrderingFinishToMessageListener!=null) {
						mOnOrderingFinishToMessageListener.onOrderingFinishToMessage(null,0, null,null);
						mOnOrderingFinishToMessageListener=null;
				      }
					// 删除数据库
					db_myordering.onDelete();
				}
			}
		}

		@Override
		public void onFailed(int status, String msg) {
			isload=false;
			if (!mOnOrderingFinishListener.isEmpty()) {
				for (int i = 0; i < mOnOrderingFinishListener.size(); i++) {
					mOnOrderingFinishListener.get(i).onOrderingFinish(null);
				}
		      }
			if (mOnOrderingFinishToMessageListener!=null) {
				mOnOrderingFinishToMessageListener.onOrderingFinishToMessage(null,0, null,null);
				mOnOrderingFinishToMessageListener=null;
		      }
		}

		@Override
		protected Object doInBackground(Object... params) {
			isload=true;
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(myOrder).getJson();
			return new MyOrderNet_Unfini(context, json);
		}
	}
	Handler mHandler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch(msg.what){
			//进行中的订单写入完成
			case BaseBBXActivity.MSG_ORDERING_WRITE:
				if (!mOnOrderingFinishListener.isEmpty()) {
					for (int i = 0; i < mOnOrderingFinishListener.size(); i++) {
						mOnOrderingFinishListener.get(i).onOrderingFinish(list);
					}
			      }
				if (mOnOrderingFinishToMessageListener!=null) {
					mOnOrderingFinishToMessageListener.onOrderingFinishToMessage(list,type,jmsg_data,data);
					mOnOrderingFinishToMessageListener=null;
			      }
				if(IsSnQiangDan.isIncludeSnQiangDan(list)!=null){
					 Publisher.getInstance().publish(GMsg.ONLINE_SNQIANGDAN,IsSnQiangDan.isIncludeSnQiangDan(list));
				}
				break;
			}
		}
	};
}
